package com.icode.testVolatile;

import jdk.nashorn.internal.ir.Block;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2020-12-14 14:53
 *
 * volatile:
 *  1、保证可见性
 *  2、不保证原子性
 *  3、禁止指令重排
 */
public class VolatileDemo01 {
    private static volatile int num = 0;

//    private static Lock lock = new ReentrantLock();
    public static void add() {
//        lock.lock();
//        try {
            num++;
//        } finally {
//            lock.unlock();
//        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int i1 = 0; i1 < 1000; i1++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount()>2){

        }
        System.out.println("num = " + num);
    }

}
